<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
		<meta name="apple-mobile-web-app-capable" content="yes"><!--删除默认的苹果工具栏和菜单栏-->
		<meta name="apple-mobile-web-app-status-bar-style" content="black" /><!--状态栏显示为黑色-->
		<meta name="format-detection" content="telephone=no" /><!--电话号码不被显示为拨号连接-->
		<title>融资融券交易终端</title>
		<link rel="stylesheet" href="<?php echo STATIC_SERVER; ?>/mobile/layer/layer.css" id="layui_layer_layercss"/>
		<link rel="stylesheet" href="<?php echo STATIC_SERVER; ?>/mobile/css/weui.css" />
		<link rel="stylesheet" href="<?php echo STATIC_SERVER; ?>/mobile/css/style.css" />
		<script src="<?php echo STATIC_SERVER; ?>/mobile/js/jquery-1.12.1.min.js"></script>
		<script src="<?php echo STATIC_SERVER; ?>/mobile/layer/layer.js"></script>
	</head>
	<body>
		<script type="text/javascript" src="<?php echo STATIC_SERVER; ?>/home/js/SuggestServer_3_0_16.js"></script>
		<div class="my-pic">
			<span>交易</span>
		</div>
		<div class="xinsu-section" style="padding-top: 43px;">
			<div class="xinsu-deal">
				<form class="weui-cells xinsu-cells" method="post">
					<div class="weui-cell">
						<div class="weui-cell__bd">
							<p>可用额度：<span class="xinsu-color-red">￥<?php echo number_format($user['amount'], 2); ?></span></p>
						</div>
					</div>
					<div class="weui-cell xinsu-cell">
						<div class="weui-cell__fl">
							<label class="weui-label">证券代码：</label>
						</div>
						<div class="weui-cell__bd">
							<input type="text" name="stock_code" id="stock_code"class="weui-input" placeholder="代码/名称/拼音" <?php if ($common_info['holiday'] == "休市" || $user['amount'] <= 0) { ?>disabled<?php } else { ?>onBlur="get_stock();"<?php } ?> />
						</div>
					</div>
					<div class="weui-cell xinsu-cell">
						<div class="weui-cell__fl">
							<label class="weui-label">证券名称：</label>
						</div>
						<div class="weui-cell__bd">
							<input type="text" name="stock_name" id="stock_name" class="weui-input xinsu-color-red" disabled>
						</div>
					</div>
					<div class="weui-cell xinsu-cell">
						<div class="weui-cell__fl">
							<label class="weui-label">当前价格：</label>
						</div>
						<div class="weui-cell__bd">
							<input type="text" name="stock_price" id="stock_price" class="weui-input xinsu-color-green" disabled>
						</div>
					</div>
					<div class="weui-cell xinsu-cell">
						<div class="weui-cell__fl">
							<label class="weui-label">证券类型：</label>
						</div>
						<div class="weui-cell__bd">
							<input type="radio" name="bull_type" value="1" checked <?php if ($common_info['holiday'] == "休市" || $user['amount'] <= 0) { ?>disabled<?php } ?>>
							<span class="xinsu-color-red">融资</span>
							<input type="radio" name="bull_type" value="2" <?php if ($common_info['holiday'] == "休市" || $user['amount'] <= 0) { ?>disabled<?php } ?>>
							<span class="xinsu-color-green">融券</span>
						</div>
					</div>
					<div class="weui-cell xinsu-cell">
						<div class="weui-cell__fl">
							<label class="weui-label">买入价格：</label>
						</div>
						<div class="weui-cell__bd">
							<input type="radio" name="price_type" value="0" checked <?php if ($common_info['holiday'] == "休市" || $user['amount'] <= 0) { ?>disabled<?php } ?>>
							<span class="xinsu-color-red">市价</span>
							<input type="radio" name="price_type" value="1" <?php if ($common_info['holiday'] == "休市" || $user['amount'] <= 0) { ?>disabled<?php } ?>>
							<span class="xinsu-color-green">委托价</span>
						</div>
					</div>
					<div class="weui-cell xinsu-cell" style="display:none;" id="xinsu-price-type">
						<div class="weui-cell__fl">
							<label class="weui-label">委托价格：</label>
						</div>
						<div class="weui-cell__bd">
							<input type="text" name="bull_price" class="weui-input xinsu-color-red" id="bull_price" placeholder="请输入委托价格">
						</div>
					</div>
					<div class="weui-cell xinsu-cell">
						<div class="weui-cell__fl">
							<label class="weui-label">可买股：</label>
						</div>
						<div class="weui-cell__bd">
							<input type="text" name="num" id="num" class="weui-input" disabled>
							<button class="weui-btn xinsu-btn1 weui-btn_primary xinsu-btn-buy" type="button" <?php if ($common_info['holiday'] == "休市" || $user['amount'] <= 0) { ?>disabled<?php } else { ?>onclick="bull_all()"<?php } ?>>全部</button>
						</div>
					</div>
					<div class="weui-cell xinsu-cell">
						<div class="weui-cell__fl">
							<label class="weui-label">可买数量：</label>
						</div>
						<div class="weui-cell__bd">
							<input type="text" name="bull_num" id="bull_num" class="weui-input" <?php if ($common_info['holiday'] == "休市" || $user['amount'] <= 0) {  } ?>>
						</div>
					</div>
					<div class="xinsu-deal-data">
					<div class="weui-flex">
					  	<div class="weui-flex__item">卖5</div>
					  	<div class="weui-flex__item"><span class="xinsu-color-green" id="xinsu-sell-50">--</span></div>
					  	<div class="weui-flex__item" id="xinsu-sell-51">--</div>
					  	<div class="weui-flex__item">买5</div>
					  	<div class="weui-flex__item"><span class="xinsu-color-red" id="xinsu-bull-50">--</span></div>
					  	<div class="weui-flex__item" id="xinsu-bull-51">--</div>
					</div>
					<div class="weui-flex">
						<div class="weui-flex__item">卖4</div>
					  	<div class="weui-flex__item"><span class="xinsu-color-green" id="xinsu-sell-40">--</span></div>
					  	<div class="weui-flex__item" id="xinsu-sell-41">--</div>
					  	<div class="weui-flex__item">买4</div>
					  	<div class="weui-flex__item"><span class="xinsu-color-red" id="xinsu-bull-40">--</span></div>
					  	<div class="weui-flex__item" id="xinsu-bull-41">--</div>
					</div>
					<div class="weui-flex">
						<div class="weui-flex__item">卖3</div>
					  	<div class="weui-flex__item"><span class="xinsu-color-green" id="xinsu-sell-30">--</span></div>
					  	<div class="weui-flex__item" id="xinsu-sell-31">--</div>
					  	<div class="weui-flex__item">买3</div>
					  	<div class="weui-flex__item"><span class="xinsu-color-red" id="xinsu-bull-30">--</span></div>
					  	<div class="weui-flex__item" id="xinsu-bull-31">--</div>
					</div>
					<div class="weui-flex">
						<div class="weui-flex__item">卖2</div>
					  	<div class="weui-flex__item"><span class="xinsu-color-green" id="xinsu-sell-20">--</span></div>
					  	<div class="weui-flex__item" id="xinsu-sell-21">--</div>
					  	<div class="weui-flex__item">买2</div>
					  	<div class="weui-flex__item"><span class="xinsu-color-red" id="xinsu-bull-20">--</span></div>
					  	<div class="weui-flex__item" id="xinsu-bull-21">--</div>
					</div>
					<div class="weui-flex">
						<div class="weui-flex__item">卖1</div>
					  	<div class="weui-flex__item"><span class="xinsu-color-green" id="xinsu-sell-10">--</span></div>
					  	<div class="weui-flex__item" id="xinsu-sell-11">--</div>
					  	<div class="weui-flex__item">买1</div>
					  	<div class="weui-flex__item"><span class="xinsu-color-red" id="xinsu-bull-10">--</span></div>
					  	<div class="weui-flex__item" id="xinsu-bull-11">--</div>
					</div>
					<div class="weui-flex xinsu-flex">
					  	<div class="weui-flex__item">现价：<span class="xinsu-color-red" id="xinsu-now-price">--</span></div>
					  	<div class="weui-flex__item">昨收：<span class="xinsu-color-green" id="xinsu-yesterday-price">--</span></div>
					</div>
					<div class="weui-flex">
					  	<div class="weui-flex__item">最高价：<span class="xinsu-color-red" id="xinsu-bull-price">--</span></div>
					  	<div class="weui-flex__item">最低价：<span class="xinsu-color-green" id="xinsu-sell-price">--</span></div>
					</div>
					</div>
					<div class="weui-btn-area xinsu-btn-area">
						<button class="weui-btn xinsu-btn weui-btn_primary" type="submit">下单</button>
					</div>
				</form>
			</div>
		</div>
		<?php if ($common_info['holiday'] != "休市" || $user['amount'] <= 0) { ?>
		<script>
			$(function(){
				//股票查询事件
				var suggestServer = new SuggestServer();
				suggestServer.bind({
					"input": "stock_code",
					"value": "@2@",
					"type": "",
					"max": 10,
					"width": 250,
					"head": ["选项", "代码", "中文名称"],
					"body": [-1, 2, 4],
					"fix": { "ie6" : [0, - 1], "ie7" : [0, - 1], "firefox" : [1, 1]},
					"callback": function(){
						stock = 0;
						get_stock();
					}
				});
			})
			var amount = "<?php echo $user['amount']; ?>";
			var stock = 0;
			
			//市价，委托价选择
			$("input[name='price_type']").click(function(){
				var price_type = $('input[name="price_type"]:checked').val();
				if (price_type == 0) {
					$("#xinsu-price-type").hide();
				} else {
					$("#xinsu-price-type").show();
					$("#bull_price").val($("#stock_price").val());
				}
			})
			
			//股票信息
			function get_stock() {
				var code = $('#stock_code').val();
				if(code.length == 6 && stock >= 0) {
					http_request_post("/stock/ajax_get_stock", {code : code}, function(result) {
						var data = result.data;
						//卖五
						$("#xinsu-sell-50").html(data[29]);
						$("#xinsu-sell-51").html(data[28]);
						//卖四
						$("#xinsu-sell-40").html(data[27]);
						$("#xinsu-sell-41").html(data[26]);
						//卖三
						$("#xinsu-sell-30").html(data[25]);
						$("#xinsu-sell-31").html(data[24]);
						//卖二
						$("#xinsu-sell-20").html(data[23]);
						$("#xinsu-sell-21").html(data[22]);
						//卖一
						$("#xinsu-sell-10").html(data[21]);
						$("#xinsu-sell-11").html(data[20]);
						
						//买一
						$("#xinsu-bull-10").html(data[19]);
						$("#xinsu-bull-11").html(data[18]);
						//买二
						$("#xinsu-bull-20").html(data[17]);
						$("#xinsu-bull-21").html(data[16]);
						//买三
						$("#xinsu-bull-30").html(data[15]);
						$("#xinsu-bull-31").html(data[14]);
						//买四
						$("#xinsu-bull-40").html(data[13]);
						$("#xinsu-bull-41").html(data[12]);
						//买五
						$("#xinsu-bull-50").html(data[11]);
						$("#xinsu-bull-51").html(data[10]);
						
						//价格
						$("#xinsu-now-price").html(data[3]);
						$("#xinsu-yesterday-price").html(data[2]);
						
						//涨停
						$("#xinsu-bull-price").html(data[4]);
						$("#xinsu-sell-price").html(data[5]);
						
						//股票信息
						$("#stock_name").val(data[0]);
						$("#stock_price").val(data[3]);

						//可买股数
						if (Math.floor(amount / data[3]) <= 0) {
							var num = 0;
						} else {
							var num = Math.floor(amount / data[3]);
						}
						$("#num").val(num);
					})
					
					stock = setTimeout("get_stock();", 1500);
				}
			}
			
			//全部买入
			function bull_all() {
				var bull_num = $("#num").val();
				$("#bull_num").val(bull_num);
			}
		</script>
		<?php } ?>
		<div class="weui-tabbar xinsu-tabbar">
		    <a href="/stock/bull" class="weui-tabbar__item <?php if ($common_info['action'] == 'bull') { ?>xinsu-current<?php } ?>">
			    <div class="weui-tabbar__icon">
			        <i class="xinsu-icon-font xinsu-icon-deal"></i>
			    </div>
		      	<p class="weui-tabbar__label">交易</p>
		    </a>
		    <a href="/stock/index" class="weui-tabbar__item <?php if ($common_info['action'] != 'bull' && $common_info['controller'] == 'stock') { ?>xinsu-current<?php } ?>">
			    <div class="weui-tabbar__icon">
			        <i class="xinsu-icon-font xinsu-icon-search"></i>
			    </div>
			    <p class="weui-tabbar__label">查询</p>
		    </a>
		    <a href="/user/asset" class="weui-tabbar__item <?php if ($common_info['action'] == 'asset' || $common_info['controller'] == 'atm') { ?>xinsu-current<?php } ?>">
		      	<div class="weui-tabbar__icon">
		        	<i class="xinsu-icon-font xinsu-icon-asset"></i>
		      	</div>
		      	<p class="weui-tabbar__label">资产</p>
		    </a>
		    <a href="/" class="weui-tabbar__item <?php if (($common_info['action'] != 'asset' && $common_info['controller'] == 'user') || $common_info['controller'] == 'index') { ?>xinsu-current<?php } ?>">
		      	<div class="weui-tabbar__icon">
		        	<i class="xinsu-icon-font xinsu-icon-user"></i>
		      	</div>
		      	<p class="weui-tabbar__label">我的</p>
		    </a>
		</div>
	</body>
	<script>
		//底部导航隐藏
		var original = document.documentElement.clientHeight;
		window.addEventListener("resize", function() {
			var resizeHeight = document.documentElement.clientHeight;
			if(resizeHeight != original) {
				$('.xinsu-tabbar').hide()
			} else {
				$('.xinsu-tabbar').show()
			}
		});
		var token = "<?php echo $token; ?>";
		//退出登录
		function logout() {
			layer.confirm('您确定要退出？', {icon: 3}, function () {
				stock = system_info = -1
				var url = "/user/logout";
				layer.closeAll();
				http_request_post(url, null, function(res) {
					layer.msg(res.msg, {time: 800}, function () {
						//跳转到登录页面
						location.href = "/user/login";
					});
				});
			});
		}
		
		//提交表单
        var sub = 0;
		$("form").submit(function () {
			//判断提交方式：get-搜索专用，post-保存专用
			var method = $(this).attr("method");
			if (method == "post") {
				//保存专用
				var rmsg = "";
				var data = $(this).serialize();
				if ("<?php echo $common_info['action']; ?>" == "bull") {
					var msg = "您是否确定以上交易？";
					var bull_type = $("input[name='bull_type']:checked").val();
					var price_type = $("input[name='price_type']:checked").val();
					
					//股票类型
					if (bull_type == 1) {
						rmsg += "融资";
					} else {
						rmsg += "融券";
					}
					
					//交易类型
					if (price_type == 0) {
						rmsg += "市价";
					} else {
						rmsg += "限价";
					}
					rmsg += "委托处理成功！";
				} else {
					var msg = "您是否确执行该操作？";
				}
				data += '&token='+token;
				layer.confirm(msg, {icon: 3}, function () {
                    sub += 1;
					layer.closeAll();
                    if (sub == 1) {
                        http_request_post("", data, function (result) {
                            if (rmsg == "") {
                                layer.msg(result.msg, {time:800}, function (){
                                    if (result.data != "") {
                                        location.href = result.data;
                                    } else {
                                        location.reload();
                                    }
                                });
                            } else {
                                layer.alert(rmsg, {icon:1, closeBtn:0}, function(){
                                    if (result.data != "") {
                                        location.href = result.data;
                                    } else {
                                        location.reload();
                                    }
                                });
                            }
                        });
                    }
				});
				return false;
			} else {
				//搜索专用
				var data = $(this).serializeArray();
				var	url = "/<?php echo $common_info['controller']; ?>/<?php echo $common_info['action']; ?>";
				$.each(data, function(key, val) {
					if (val.value == "") {
						url += "/0";
					} else {
						url += "/" + val.value;
					}
				});
				location.href = url;
				return false;
			}
		})
		
		//通用post请求
		function http_request_post(url, data, callback) {
			if (url != "") {
				url = url;
			}
			$.ajax({
				type : 'post',  
				url : url,
				dataType : 'json',
				data : data,
				success : function(repones) {
					if (repones.status == 1) {
						if (typeof callback === "function") {
							callback(repones);
							return false;
						} else {
							layer.msg('请求加载数据失败，请重新加载');
						}
					} else if (repones.status == 2) {
						stock = system_info = -1
						layer.msg(repones.msg, {time:800}, function() {
							location.href = "/user/login";
						});
						return false;
					} else if (repones.status == 0) {
						token = repones.token;
						layer.msg(repones.msg);
						stock = system_info = -1
						return false;
					} else {
						layer.msg(repones.msg);
						stock = system_info = -1
						return false;
					}
				},
				error : function () {
					layer.load(1, {shade: [0.1,'#fff'], time:500}, function(){
						location.reload();
					});
					return false;
				}
			});
			return false;
		}
	</script>
</html>